package com.quanxiaoha.weblog.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quanxiaoha.weblog.admin.model.vo.comment.FindCommentPageListReqVO;
import com.quanxiaoha.weblog.admin.model.vo.comment.FindCommentPageListRespVO;
import com.quanxiaoha.weblog.admin.model.vo.comment.UpdateCommentStatusReqVO;
import com.quanxiaoha.weblog.admin.service.AdminCommentService;
import com.quanxiaoha.weblog.common.PageResponse;
import com.quanxiaoha.weblog.common.Response;
import com.quanxiaoha.weblog.common.domain.dos.ArticleDO;
import com.quanxiaoha.weblog.common.domain.dos.CommentDO;
import com.quanxiaoha.weblog.common.domain.mapper.ArticleMapper;
import com.quanxiaoha.weblog.common.domain.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminCommentServiceImpl implements AdminCommentService {

    @Autowired
    private CommentMapper commentMapper;
    
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Response findCommentPageList(FindCommentPageListReqVO findCommentPageListReqVO) {
        Integer current = findCommentPageListReqVO.getCurrent();
        Integer size = findCommentPageListReqVO.getSize();

        // 分页查询
        Page<CommentDO> page = new Page<>(current, size);
        LambdaQueryWrapper<CommentDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(CommentDO::getCreateTime);
        
        IPage<CommentDO> result = commentMapper.selectPage(page, wrapper);
        
        List<CommentDO> records = result.getRecords();
        
        List<FindCommentPageListRespVO> vos = records.stream()
                .map(commentDO -> {
                    // 查询文章标题
                    String articleTitle = null;
                    if (commentDO.getArticleId() != null) {
                        ArticleDO articleDO = articleMapper.selectById(commentDO.getArticleId());
                        if (articleDO != null && StringUtils.hasText(articleDO.getTitle())) {
                            articleTitle = articleDO.getTitle();
                        }
                    }
                    
                    
                    return FindCommentPageListRespVO.builder()
                            .id(commentDO.getId())
                            .articleId(commentDO.getArticleId())
                            .articleTitle(articleTitle)
                            .parentId(commentDO.getParentId())
                            .nickname(commentDO.getNickname())
                            .avatar(commentDO.getAvatar())
                            .content(commentDO.getContent())
                            .ipAddress(commentDO.getIpAddress())
                            .createTime(commentDO.getCreateTime())
                            .status(commentDO.getStatus())
                            .isChecked(commentDO.getIsChecked()) // 添加审核状态字段
                            .build();
                })
                .collect(Collectors.toList());

        return PageResponse.success(result, vos);
    }

    @Override
    public Response updateCommentStatus(UpdateCommentStatusReqVO updateCommentStatusReqVO) {
        CommentDO commentDO = CommentDO.builder()
                .id(updateCommentStatusReqVO.getId())
                .status(updateCommentStatusReqVO.getStatus())
                .build();
        
        // 如果有审核状态，则也更新审核状态
        if (updateCommentStatusReqVO.getIsChecked() != null) {
            commentDO.setIsChecked(updateCommentStatusReqVO.getIsChecked());
        }
        
        commentMapper.updateById(commentDO);
        return Response.success();
    }
}